# 翻转一棵二叉树。
#
#  示例：
#  输入：
#       4
#    /   \
#   2     7
#  / \   / \
# 1   3 6   9
#
#  输出：
#      4
#    /   \
#   7     2
#  / \   / \
# 9   6 3   1
from com.example.tree.tree_node import TreeNode


class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        """
        后序遍历
        :param root:
        :return:
        """
        if not root:
            return root
        leftRes = self.invertTree(root.left)
        rightRes = self.invertTree(root.right)
        root.left, root.right = rightRes, leftRes
        return root


if __name__ == "__main__":
    root = TreeNode(4)
    root.left, root.right = TreeNode(2), TreeNode(7)
    root.left.left, root.left.right = TreeNode(1), TreeNode(3)
    root.right.left, root.right.right = TreeNode(6), TreeNode(9)
    res = Solution().invertTree(root)
    lst = []
    def traversal(node) -> None:
        if not node:
            return
        lst.append(node.val)
        traversal(node.left)
        traversal(node.right)
    traversal(res)
    print(lst)
